Use only the lowest 32 bit of input for 32 bit division operations #100
+22
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
The RISCV implementations (sol/fast/slow) return incorrect results, in conflict with the RISCV specification, for all 32-bit division operations (DIVW, DIVUW, REMW, REMUW) when the divisor is zero.
All word (32-bit) arithmetic operations are supposed to take as its operands only the lowest 32 bits of the input registers.
However, in the division by zero code that requirement fails.
Observe that the switch statement only catches the case in which the input register has all bits set to zero. In reality, only the least significant 32 bits should be considered.
In the case any of the most significant 32 bits is set, the
switch
statement will follow the default path, and the division will be performed with the same behavior as an EVM division (except for sign extending) -- that means the division by zero will result in a zero result, instead of the correct RISCV one (all bits set for division, or the original numerator for remainder).Mask the input before the switch statement.